package com.kwok.openplatform.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//登录拦截器
public class LoginInterceptor implements HandlerInterceptor {

    //访问目标handler之前触发
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取登录的用户名
        String loginFlag = (String) request.getSession().getAttribute("loginFlag");
        String username = (String) request.getSession().getAttribute("username");
        //如果用户名为空，则说明没有登录凭证，跳转到登录页面
        if (loginFlag == null && username == null) {
            response.sendRedirect("/login.html");
            return false;
        }
        //此处返回true表示放行，返回false表示进行拦截
        return true;
    }

    //访问目标handler之后触发
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
//    }

    //视图渲染完成之后触发
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
//    }

}
